package com.easylinkin.linkappapi.common.service;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.easylinkin.linkappapi.common.utils.DateUtil;
import com.easylinkin.linkappapi.operatelog.entity.ObjectOperateLog;
import com.easylinkin.sm.context.AdminAccountContext;
import com.easylinkin.sm.context.SmUserContextProducer;
import com.easylinkin.sm.entity.User;

import site.morn.framework.context.AccountContext;
import site.morn.framework.entity.BaseUser;

/**
 * <p></p>
 *
 * @author liuming
 * @since 2020/5/20
 */
@Component
public class CommonAdminServiceImpl implements CommonAdminService {

    Logger LOGGER = LoggerFactory.getLogger(CommonAdminServiceImpl.class);
    @Resource
    SmUserContextProducer smUserContextProducer;

    @Override
    public void setCreateAndModifyInfos(Collection objects) {
        if (CollectionUtils.isEmpty(objects)) {
            LOGGER.info("要设置创建修改信息对象集合为空");
            return;
        }
        BaseUser baseUser = AdminAccountContext.currentUser();
        if (baseUser == null) {
            throw new RuntimeException("登录用户信息为null");
        }
        try {
            for (Object object : objects) {
                setCreateModifyInfo(baseUser, object);
            }

        } catch (Exception e) {
            LOGGER.error("setCreateAndModifyInfos失败：", e);
            throw new RuntimeException("设置创建和修改人方法执行失败");
        }
    }

    @Override
    public void setModifyInfos(Collection objects) {
        if (CollectionUtils.isEmpty(objects)) {
            LOGGER.info("要设置修改信息对象集合为空");
            return;
        }
        BaseUser baseUser = AccountContext.currentUser();
        if (baseUser == null) {
            throw new RuntimeException("登录用户信息为null");
        }
        try {
            for (Object object : objects) {
                setModifyInfo(baseUser, object);
            }

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException("设置修改人方法执行失败");
        }
    }

    private void setCreateModifyInfo(BaseUser baseUser, Object object)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        object.getClass().getMethod("setCreator", String.class)
            .invoke(object, String.valueOf(baseUser.getId()));
        object.getClass().getMethod("setCreateTime", Date.class)
            .invoke(object, DateUtil.now());
        setModifyInfo(baseUser, object);
    }

    private void setModifyInfo(BaseUser baseUser, Object object)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

        object.getClass().getMethod("setModifier", String.class)
            .invoke(object, String.valueOf(baseUser.getId()));
        object.getClass().getMethod("setModifyTime", Date.class)
            .invoke(object, DateUtil.now());
    }

    @Override
    public void setCreateAndModifyInfo(Object obj) {
        BaseUser baseUser = AdminAccountContext.currentUser();
        if (baseUser == null) {
        	return;
        }
        try {
            setCreateModifyInfo(baseUser, obj);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException("设置创建和修改人方法执行失败");
        }
    }


    @Override
    public void setModifyInfo(Object obj) {
        BaseUser baseUser = AdminAccountContext.currentUser();
        if (baseUser == null) {
            return;
        }
        try {
            setModifyInfo(baseUser, obj);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException("设置修改人修改时间方法执行失败");
        }
    }

    @Override
    public void setLogUserInfo(ObjectOperateLog objectOperateLog) {
    	User user = smUserContextProducer.getCurrent();
        objectOperateLog.setCreator(user.getId().toString());

        if (StringUtils.isNotEmpty(user.getNickname())) {
            objectOperateLog.setCreatorName(user.getNickname());
        } else {
            objectOperateLog.setCreatorName(user.getUsername());
        }
        objectOperateLog.setCreateTime(new Date());
    }

}
